package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.model.TbContent;
import com.dongdongshop.model.TbContentCategory;
import com.dongdongshop.model.TbContentExample;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.uitl.RedisUtils;
import com.dongdongshop.vo.ContentVO;
import com.google.common.base.Equivalence;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper contentMapper;
    @Resource
    private TbContentCategoryMapper contentCategoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Cacheable(value="dk22070")
    @Override
    public List<ContentVO> getAll() {
        List<TbContent> tbContents = contentMapper.selectByExample(null);
        List<ContentVO> list = new ArrayList();

        for (TbContent tbContent : tbContents) {
            ContentVO vo = new ContentVO();

            //广告ID
            vo.setId(tbContent.getId());

            //标题
            vo.setTitle(tbContent.getTitle());

            //url
            vo.setUrl(tbContent.getUrl());

            //pic
            vo.setPic(tbContent.getPic());

            //排序
            vo.setSortOrder(tbContent.getSortOrder());

            //分类
            if (tbContent.getCategoryId() == null) {
                vo.setCategoryId2(null);
            }else {
                TbContentCategory cc = contentCategoryMapper.selectByPrimaryKey(tbContent.getCategoryId());
//                Long aLong = Long.valueOf(cc.getName());
                vo.setCategoryId2(cc.getName());
            }

            //状态
            if (tbContent.getStatus().equals("2")){
                vo.setStatus("无效");
            }
            if (tbContent.getStatus().equals("1")){
                vo.setStatus("有效");
            }

            list.add(vo);
        }
        return list;
//        return contentMapper.selectByExample(null);
    }
    @CachePut(value="dk22071")
    @Override
    public void addContent(ContentVO vo) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(vo,content);
        contentMapper.insertSelective(content);
    }

    @Override
    public ContentVO toUpdate(Long id) {
        TbContent content = contentMapper.selectByPrimaryKey(id);
        ContentVO vo = new ContentVO();
        BeanUtils.copyProperties(content,vo);
        return vo;
    }

    @CacheEvict(value="dk22072")
    @Override
    public void updateContent(ContentVO vo) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(vo,content);
        contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public List<TbContent> selectByIdLunBo(Long categoryId) {
        HashOperations hash1 = redisTemplate.opsForHash();
        Boolean aBoolean = hash1.hasKey(RedisUtils.SLIDE_KEY, RedisUtils.SLIDE_FIELD);

        if (aBoolean){
            //从redis中取
            List<TbContent> redisList = (List<TbContent>)hash1.get(RedisUtils.SLIDE_KEY, RedisUtils.SLIDE_FIELD);
            return redisList;
        }else{
            //从数据库中取
            TbContentExample example1 = new TbContentExample();
            TbContentExample.Criteria criteria = example1.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = contentMapper.selectByExample(example1);
            hash1.put(RedisUtils.SLIDE_KEY, RedisUtils.SLIDE_FIELD,tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByIdNew(Long categoryId) {
        HashOperations hash2 = redisTemplate.opsForHash();
        Boolean aBoolean = hash2.hasKey(RedisUtils.DAILY_KEY, RedisUtils.DAILY_FIELD);
        if (aBoolean){
            List<TbContent> redisList = (List<TbContent>)hash2.get(RedisUtils.DAILY_KEY, RedisUtils.DAILY_FIELD);
            return redisList;
        }else{
            //从数据库中取
            TbContentExample example2 = new TbContentExample();
            TbContentExample.Criteria criteria = example2.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = contentMapper.selectByExample(example2);
            hash2.put(RedisUtils.DAILY_KEY, RedisUtils.DAILY_FIELD,tbContents);
            return tbContents;
        }
    }

    @Override
    public List<TbContent> selectByIdLove(Long categoryId) {
        HashOperations hash3 = redisTemplate.opsForHash();
        Boolean aBoolean = hash3.hasKey(RedisUtils.LOVE_KEY, RedisUtils.LOVE_FIELD);
        if (aBoolean){
            List<TbContent> redisList = (List<TbContent>)hash3.get(RedisUtils.LOVE_KEY, RedisUtils.LOVE_FIELD);
            return redisList;
        }else{
            //从数据库中取
            TbContentExample example3 = new TbContentExample();
            TbContentExample.Criteria criteria = example3.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = contentMapper.selectByExample(example3);
            hash3.put(RedisUtils.LOVE_KEY, RedisUtils.LOVE_FIELD,tbContents);
            return tbContents;
        }
    }

    @CacheEvict(value="dk22072")
    @Override
    public int deleteBatch(Long[] ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        List<Long> list = Arrays.asList(ids);
        criteria.andIdIn(list);
        return contentMapper.deleteByExample(example);
    }
}
